package com.hdu.dwh.controller;


import com.hdu.dwh.dtos.DatawarehouseDatabaseDto;
import com.hdu.dwh.pojos.source_access_pojos.DataWarehouseDatabase;
import com.hdu.dwh.service.DataWarehouseDatabaseService;
import com.hdu.dwh.vos.dataWareHouse_vos.DataWarehouseDatabaseDetailVo;
import com.hdu.dwh.results.ResponseResult;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;

import java.util.Set;

import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.PathVariable;


/**
 * <p>
 * 源数据库表 前端控制器
 * </p>
 *
 * @author bobo
 * @since 2025-04-08
 */
@RestController
@RequestMapping("/dwh/database")
@RequiredArgsConstructor
@Tag(name = "湖仓数据库管理")
public class DataWarehouseDatabaseController {

    private final DataWarehouseDatabaseService dwhDatabaseService;

    @Operation(summary ="查询详细信息")
    @GetMapping("/{id}")
    public ResponseResult<?> getDetailById(@PathVariable Long id) {
        return dwhDatabaseService.getDetailById(id);
    }

    @Operation(summary = "删除湖仓数据库")
    @DeleteMapping("/{databaseName}")
    public ResponseResult<?> deleteByDatabaseName(@PathVariable String databaseName) throws Exception {
        dwhDatabaseService.deleteByDatabaseName(databaseName);
        return ResponseResult.success("删除湖仓数据库成功");
    }

    @Operation(summary = "新增湖仓数据库")
    @PostMapping("/addDatabase")
    public ResponseResult<DataWarehouseDatabase> addDatabase(@Valid @RequestBody DatawarehouseDatabaseDto dwhDatabaseDto) throws Exception {
        // TODO: paimon和数据库的数据不同步，待解决
        return ResponseResult.success(dwhDatabaseService.createPaimonDatabase(dwhDatabaseDto));
    }

    @Operation(summary = "查询湖仓所有数据库详细信息")
    @GetMapping("/getAllDatabaseDetail")
    public ResponseResult<Set<DataWarehouseDatabaseDetailVo>> listDwhDatabaseDetail() {
        return ResponseResult.success(dwhDatabaseService.listDwhDatabaseDetail());
    }

    @Operation(summary = "更新湖仓数据库信息")
    @PutMapping("{id}")
    public ResponseResult<DataWarehouseDatabase> updateDatabase(@PathVariable Integer id, @RequestBody DatawarehouseDatabaseDto entity) throws Exception {
        DataWarehouseDatabase dwbDatabase = dwhDatabaseService.updateDatabase(id, entity);
        return ResponseResult.success(dwbDatabase);
    }

}
